/**
 * SpringMVC restful 风格
 * 用户控制器，控制关于代金券的一些业务逻辑操作
 * @author WangXinLei
 * @date 2014-12-01
 */
package com.chnbst.bstyun.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.Pager;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.eos.CreateNum;
import com.chnbst.bstyun.model.pojo.AdminUser;
import com.chnbst.bstyun.model.pojo.Goods;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.pojo.Voucher;
import com.chnbst.bstyun.model.vo.VoucherVo;
import com.chnbst.bstyun.service.GoodsService;
import com.chnbst.bstyun.service.UserService;
import com.chnbst.bstyun.service.VoucherService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author WangXinLei
 *
 */
@Controller
@RequestMapping("/sysAdmin")
public class VoucherAdminController {

	//每页显示的用户数
	private int pageSize = 10;
	//用户列表页码
	private int currPage = 1;

	@Autowired
	private VoucherService voucherService;
	@Autowired
	private UserService userService;
	@Autowired
	private CreateNum createNum;
	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 代金券主页面展现
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/voucher/voucherList",method=RequestMethod.GET)
	public ModelAndView voucherList()throws Exception{
		return new ModelAndView("/admin/financeManager/voucherList");
	}
	
	/**
	 * 代金券下拉框 企业用户查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/voucher/companyList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> companyList()throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleId", 0);
		map.put("newRecords", 0);
		List<User> companyList =userService.selectByRoleId(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("items", companyList);
		return resultMap;
	}
	
	/**
	 * 代金券下拉框  个人用户查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/voucher/userList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userList()throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleId", 1);
		map.put("newRecords", 0);
		List<User> userList =userService.selectByRoleId(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("items", userList);
		return resultMap;
	}
	
	/**
	 * 代金券下拉框 套餐和产品查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/voucher/mpList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> mpList()throws Exception{
		Map<String, Object> resmap = new HashMap<String, Object>();
		List<Goods> list = goodsService.selectAllGoods(new HashMap<String, Object>());
		resmap.put("items", list);
		return resmap;
	}
	
	/**
	 * 代金券下拉框  企业用户  搜索框 模糊搜索
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/voucher/cListSearch",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> cListSearch(String value)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleId", 0);
		map.put("userEmail", value);
		List<User> companyList =userService.selectByRoleId(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("items", companyList);
		return resultMap;
	}
	
	/**
	 * 代金券下拉框  个人用户  搜索框 模糊搜索
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/voucher/uListSearch",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> uListSearch(String value)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleId", 1);
		map.put("newRecords", 0);
		map.put("userEmail", value);
		List<User> userList =userService.selectByRoleId(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("items", userList);
		return resultMap;
	}
	
	/**
	 * 代金券下拉框  套餐和产品用户  搜索框 模糊搜索
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/voucher/mpListSearch",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> mpListSearch(String value)throws Exception{
		Map<String, Object> resmap = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("goodsName", value);
		List<Goods> list = goodsService.selectAllGoods(map);
		resmap.put("items", list);
		return resmap;
	}
	
	
	
	@RequestMapping(value="/voucher/voucherList/search",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> search(String page,String last,Integer state,String vnumber)throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(SignUtil.isNotEmpty(page)) {
			currPage = Integer.valueOf(page);
		}
		if(SignUtil.isNotEmpty(last)) {
			pageSize = Integer.valueOf(last);
		}
		if(state != null)
			map.put("state", state);
		if(vnumber != null && !vnumber.equals(""))
			map.put("voucherNum", vnumber);
		int totalRows = voucherService.selectVouchersCount(map);
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < currPage) {
			currPage = pageCount;
		}
		int start = Pager.getStart(currPage, pageSize);
		map.put("start", start);
		map.put("pageSize", pageSize);
		map.put("sortField", "voucher_id");
		map.put("sortOrder", "desc");
		if(state != null)
			map.put("state", state);
		if(vnumber != null && !vnumber.equals(""))
			map.put("voucherNum", vnumber);
		
		List<VoucherVo> voucherList = voucherService.selectAllVouchers(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("page", currPage);
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("voucherList", voucherList);
		
		return resultMap;
	}
	
	/**
	 * @return 代金券增加
	 * @throws Exception
	 */
	@RequestMapping(value = "/operate/voucher/addVoucher", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addVoucher(String voucherNum,Double voucherMoney,
		String createTime,String onsetTime,String deadTime,Integer voucherType,
		Integer state,Integer boundType,String boundId,HttpServletRequest request) throws Exception {
	
		AdminUser adminUser = (AdminUser) request.getSession().getAttribute("adminUser");
		
		boolean isSuccess = false;
		
		Voucher voucher = new Voucher();
		String _voucherNum = voucherNum;
		if(SignUtil.isEmpty(voucherNum)){
			_voucherNum = createNum.createVoucherNum(voucherType);
		}
		voucher.setVoucherNum(_voucherNum);
		voucher.setVoucherMoney(voucherMoney);
		voucher.setCreateId(adminUser.getAdminId());
		voucher.setCreateTime(SignUtil.str2Date(createTime, "yyyy-MM-dd hh:mm:ss"));
		voucher.setOnsetTime(java.sql.Date.valueOf(onsetTime));
		voucher.setDeadTime(java.sql.Date.valueOf(deadTime));
		voucher.setVoucherType(voucherType);
		voucher.setState(state);
		voucher.setBoundType(boundType);
		voucher.setBoundId(boundId);
		
		if(SignUtil.isNotEmpty(voucherNum)){
			isSuccess = voucherService.updateVoucher(voucher);
		}else{
			isSuccess = voucherService.insertVoucher(voucher);
		}
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("voucher", voucher);
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
	/**
	 * @return 查询代金券 (未用到)
	 * @throws Exception
	 */
	@RequestMapping(value = "/voucher/searchVoucher", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> searchVoucher(String vnumber,HttpServletRequest request) throws Exception {
	
		boolean isSuccess = false;
		
		Voucher voucher = voucherService.selectByVoucherNum(vnumber);
		if(voucher != null) isSuccess = true;
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("voucher", voucher);
		resultMap.put("isSuccess", isSuccess);
		return resultMap;
	}
	
}
